package com.millstein.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadFileStreamRequest;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadFileStreamResponse;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.millstein.servicebase.handler.exceptionhandler.GuliException;
import com.millstein.vod.config.AliyunConfig;
import com.millstein.vod.service.VodService;
import com.millstein.vod.utils.InitVodClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Service
@EnableConfigurationProperties(AliyunConfig.class)
public class VodServiceImpl implements VodService {

    @Autowired
    private AliyunConfig aliyunConfig;

    @Override
    public String upload(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        // title是上传后的文件名称
        String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));
        // fileName是上传前的文件名称
        String fileName = originalFilename;
        InputStream is = null;
        try {
            is = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
            is = null;
        }
        UploadStreamRequest request = new UploadStreamRequest(aliyunConfig.getKeyid(), aliyunConfig.getKeysecret()
                , title, fileName, is);
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
        if (response.isSuccess()) {
            System.out.print("VideoId=" + response.getVideoId() + "\n");
        } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            System.out.print("VideoId=" + response.getVideoId() + "\n");
            System.out.print("ErrorCode=" + response.getCode() + "\n");
            System.out.print("ErrorMessage=" + response.getMessage() + "\n");
        }
        return response.getVideoId();
    }

    @Override
    public void remove(String id) {

        try {
            // 初始化客户端对象
            DefaultAcsClient client = InitVodClient.initVodClient(aliyunConfig.getKeyid(), aliyunConfig.getKeysecret());
            // 创建相关request和response
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 设置需要删除的视频id
            request.setVideoIds(id);
            // 调用方法实现删除
            client.getAcsResponse(request);
            // TODO 调用service-edu中的方法清空上传video表中对应的上传字段
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001, "删除视频失败");
        }
    }
}
